//初始默认银行卡信息
$.ajax({
    url:'/user-wallet.json',
    type: 'get',
    data:{
        type:1
    },
    dataType:"json",
    success: function (data) {
        if(!data.wallet){
            console.log(data)
            $('.bankcard').css('display','none');
            $('.bankcard-not').css('display','block');
            var arr = eval(data.bankList);
            for (var i in arr){
            console.log(i);
                $('.bank-type').html(i.name);
            }
        }else{
            console.log(data);
            $('.bankcard').css('display','block');
            $('.bankcard-not').css('display','none');
            $('.carded').val(data.wallet.value);
            if(data.wallet.bankId==1){
               $('.openinged').html('招商银行');
            }else if(data.wallet.bankId==2){
                $('.openinged').html('中国银行');
            }else{
                $('.openinged').html('工商银行');
            }
            $('.to-openinged').each(function(){
                if($(this).val()==data.wallet.bankId){
                    console.log($(this));
                        $(this).attr('selected','selected');
                    }
            })
            $('.user-id').html(data.wallet.id);
            $('.cardholdered').val(data.wallet.name);
        }
    },
    error:function (err) {
        // $('.err-info').html(JSON.parse(err.responseText).errors[0].msg);
    }
})
//点击切换至虚拟钱包
$('.tab-wallet').click(function(){
    $('.account-tab a').removeClass('tab-active');
    $(this).addClass('tab-active');
    $('.bankcard').css('display','none');
    $('.wallet').css('display','block');
    $('.bankcard-not').css('display','none');
    $.ajax({
        url:'/user-wallet.json',
        type: 'get',
        data:{
            type:2
        },
        dataType:"json",
        success: function (data) {
        if(!data.wallet){
            $('.wallet').css('display','none');
            $('.wallet-not').css('display','block');

        }else{
            console.log(data);
             $('.wallet').css('display','block');
             $('.wallet-not').css('display','none');
            $('.account-name').val(data.wallet.name);
            $('.virtual-currency').val(data.wallet.value);
            $('.user-id').html(data.wallet.id);
        }

        },
        error:function (msg) {
            console.log(msg)
        }
    })
});
//点击切换至银行卡
$('.tab-bankcard').click(function(){
    $('.account-tab a').removeClass('tab-active');
    $(this).addClass('tab-active');
    $('.bankcard').css('display','block');
    $('.wallet').css('display','none');
    $('.wallet-not').css('display','none');
    $.ajax({
        url:'/user-wallet.json',
        type: 'get',
        data:{
            type:1
        },
        dataType:"json",
        success: function (data) {
            if(!data.wallet){
                $('.bankcard').css('display','none');
                $('.bankcard-not').css('display','block');
                var arr = eval(data.bankList);
                    console.log(arr)
                    for (var i in arr){
                    console.log(i);
                        $('.bank-type').html(i.name);
                 }
            }else{
                console.log(data);
                $('.bankcard').css('display','block');
                $('.bankcard-not').css('display','none');
                $('.carded').val(data.wallet.value);
                if(data.wallet.bankId==1){
                   $('.openinged').html('招商银行');
                }else if(data.wallet.bankId==2){
                    $('.openinged').html('中国农业银行');
                }else{
                    $('.openinged').html('工商银行');
                }
                $('.cardholdered').val(data.wallet.name);
            }
        },
        error:function (msg) {
            console.log(msg)
        }
    })
})
//银行卡绑定按钮
$('.binding-btn').click(function(){
    var cardholder = $('.cardholder').val();
    var opening = $("#opening option:selected").val();
    var card = $('.card').val();
    var confirm = $('.confirm').val();
    if(!cardholder){
        $('.info-err').html('持卡人姓名不能为空');
        return false;
    }
    if(!opening){
        $('.info-err').html('请选择开户行');
        return false;
    }
    if(!card){
        $('.info-err').html('银行卡号不能为空');
        return false;
    }
    if(!card==confirm){
        $('.info-err').html('两次输入银行卡账号不一致');
        return false;
    }
    var data = {
        name:cardholder,
        bankId:opening,
        value:card,
        reBankCardNumber:confirm,
        type:1
    };
    if(cardholder && opening && card && card==confirm){
        $.ajax({
           url:"/user-wallet.json",
           type:"put",
              data:JSON.stringify(data),
            contentType:'application/json;charset=UTF-8',
           success:function(data){
                console.log(data)
                $('.info-err').html('绑定成功');
            },
            error: function(msg) {
                console.log(msg)
            }
        })
    }
});
//绑定虚拟钱包
$('.wallet-btn').click(function(){
    var account = $('.account-name-not').val()
        var currency = $('.virtual-currency-not').val();
        if(!account){
            $('.info-err-wallet').html('所属人姓名不能为空');
            return false;
        }
        if(!currency){
            $('.info-err-wallet').html('虚拟币地址不能为空');
            return false;
        }
        var data = {
            name:account,
            value:currency,
            type:2
        };
        if(account && currency){
            $.ajax({
               url:"/user-wallet.json",
               type:"put",
                  data:JSON.stringify(data),
                contentType:'application/json;charset=UTF-8',
               success:function(data){
                    console.log(data)
                    $('.info-err-wallet').html('绑定成功');
                },
                error: function(msg) {
                    console.log(msg)
                }
            })
        }
});
//修改银行卡账户
$('.card-modified').click(function(){
    var cardholder = $('.cardholdered').val();
    var opening = $("#openings option:selected").val();
    var card = $('.carded').val();
    if(!cardholder){
        $('.info-err').html('持卡人姓名不能为空');
        return false;
    }
    if(!opening){
        $('.info-err').html('请选择开户行');
        return false;
    }
    if(!card){
        $('.info-err').html('银行卡号不能为空');
        return false;
    }
    var data = {
        name:cardholder,
        bankId:opening,
        value:card,
        reBankCardNumber:confirm,
        type:1,
        id:Number($('.user-id').html())
    };
    if(cardholder && opening && card ){
        $.ajax({
           url:"/user-wallet.json",
           type:"put",
              data:JSON.stringify(data),
            contentType:'application/json;charset=UTF-8',
           success:function(data){
                console.log(data)
                $('.info-err-card').html('修改成功');
            },
            error: function(msg) {
                console.log(msg)
            }
        })
    }
});
//修改虚拟钱包地址
$('.vir-modified').click(function(){
    var account = $('.account-name').val()
    var currency = $('.virtual-currency').val();
    if(!account){
        $('.info-err-vir').html('所属人姓名不能为空');
        return false;
    }
    if(!currency){
        $('.info-err-vir').html('虚拟币地址不能为空');
        return false;
    }
    var data = {
        name:account,
        value:currency,
        type:2,
        id:Number($('.user-id').html())
    };
    if(account && currency){
        $.ajax({
           url:"/user-wallet.json",
           type:"put",
              data:JSON.stringify(data),
            contentType:'application/json;charset=UTF-8',
           success:function(data){
                console.log(data)
                $('.info-err-vir').html('修改成功');
            },
            error: function(msg) {
        $('.info-err-vir').html(JSON.parse(msg.responseText).errors[0].msg);
            }
        })
    }
});
